package ldkxingzhe.animation.chapter5;

import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RectShape;

/**
 * Created by ke on 15-11-3.
 */
public class OrreryDrawable extends LayerDrawable {
    private final String TAG = OrreryDrawable.class.getSimpleName();

    // MARK: CONSTANTS
    private static final int SPACE_HEIGHT = 150 * 6;
    private static final int RADIUS_SUN = 20 * 6;
    private static final int RADIUS_EARTH = 10 * 6;
    private static final int RADIUS_MOON = 3 * 6;
    private static final int ORBIT_EARTH = 50 * 6;
    private static final int ORBIT_MOON = 20 * 6;
    private static final int SPACE_ID = 0;
    private static final int SUN_ID = 1;
    private static final int EARTH_ID = 2;
    private static final int MOON_ID = 3;

    // MARK: global variable
    private float rotationEarth = 0;
    private float rotationMoon = 0;

    public OrreryDrawable(Drawable[] layers) {
        super(layers);
    }

    public static OrreryDrawable create(){
        ShapeDrawable space = new ShapeDrawable(new RectShape());
        space.getPaint().setColor(Color.BLACK);
        space.setIntrinsicHeight(SPACE_HEIGHT);
        space.setIntrinsicWidth(SPACE_HEIGHT);

        ShapeDrawable sun = new ShapeDrawable(new OvalShape());
        sun.getPaint().setColor(Color.YELLOW);
        sun.setIntrinsicWidth(RADIUS_SUN * 2);
        sun.setIntrinsicHeight(RADIUS_SUN * 2);

        ShapeDrawable earth = new ShapeDrawable(new OvalShape());
        earth.getPaint().setColor(Color.BLUE);
        earth.setIntrinsicWidth(RADIUS_EARTH * 2);
        earth.setIntrinsicHeight(RADIUS_EARTH * 2);

        ShapeDrawable moon = new ShapeDrawable(new OvalShape());
        moon.getPaint().setColor(Color.LTGRAY);
        moon.setIntrinsicWidth(RADIUS_MOON * 2);
        moon.setIntrinsicHeight(RADIUS_MOON * 2);

        Drawable[] bodies = {space, sun, earth, moon};
        OrreryDrawable tmpOrreryDrawable = new OrreryDrawable(bodies);
        tmpOrreryDrawable.setEarthPosition(0);
        tmpOrreryDrawable.setMoonPosition(0);
        tmpOrreryDrawable.setLayerInset(SPACE_ID, 0, 0, 0,0);
        int tmp = SPACE_HEIGHT/2 - RADIUS_SUN;
        tmpOrreryDrawable.setLayerInset(SUN_ID, tmp,tmp, tmp, tmp);
        return tmpOrreryDrawable;
    }

    public void setEarthPosition(float rotationEarth) {
        this.rotationEarth = rotationEarth;
        Point center = getEarthCenter();
        setLayerInset(EARTH_ID,
                center.x - RADIUS_EARTH,
                center.y - RADIUS_EARTH,
                SPACE_HEIGHT - center.x - RADIUS_EARTH,
                SPACE_HEIGHT - center.y - RADIUS_EARTH
                );
        this.onBoundsChange(getBounds());
    }

    private Point getEarthCenter() {
        Point center = new Point();
        center.x = (int)(SPACE_HEIGHT/2 + ORBIT_EARTH * Math.sin(rotationEarth));
        center.y = (int)(SPACE_HEIGHT/2 + ORBIT_EARTH * Math.cos(rotationEarth));
        return center;
    }

    public void setMoonPosition(float rotationMoon) {
        this.rotationMoon = rotationMoon;
        Point center = getMoonCenter();
        setLayerInset(MOON_ID,
                center.x - RADIUS_MOON,
                center.y - RADIUS_MOON,
                SPACE_HEIGHT - center.x - RADIUS_MOON,
                SPACE_HEIGHT - center.y - RADIUS_MOON
        );
        this.onBoundsChange(getBounds());
    }

    private Point getMoonCenter() {
        Point center = new Point();
        Point earthCenter = getEarthCenter();
        center.x = (int)(earthCenter.x + ORBIT_MOON * Math.sin(rotationMoon));
        center.y = (int)(earthCenter.y + ORBIT_MOON * Math.cos(rotationMoon));
        return center;
    }


}
